/*!
 * Site: NorthRockHill.com
 * Author: Tyler Gordon
 *
 * @depends foundation/jquery.js
 * @depends foundation/app.js
 * @depends foundation/jquery.foundation.accordion.js
 * @depends foundation/jquery.foundation.alerts.js
 * @depends foundation/jquery.foundation.buttons.js
 * @depends foundation/jquery.foundation.forms.js
 * @depends foundation/jquery.foundation.mediaQueryToggle.js
 * @depends foundation/jquery.foundation.navigation.js
 * @depends foundation/jquery.foundation.orbit.js
 * @depends foundation/jquery.foundation.reveal.js
 * @depends foundation/jquery.foundation.tabs.js
 * @depends foundation/jquery.foundation.tooltips.js
 * @depends foundation/jquery.js
 * @depends foundation/jquery.offcanvas.js
 * @depends foundation/jquery.placeholder.js
 * @depends waypoints.min.js
 * @depends jquery.hoverIntent.min.js
 * @depends jquery.scrollTo.min.js
 * @depends ../../../js/script.js
 */

$(document).ready(function() {

  // --------------------------------------------------
  // WAYPOINTS plugin
  // --------------------------------------------------
  var sticky_nav_container = $(".sticky-container");
  var sticky_nav = $(".sticky-container nav");
  var sticky_top_spacing = 0;
  var waypoint_offset = 50;

  sticky_nav_container.waypoint({
    handler: function(event, direction) {
      if( direction == 'down' ) {
        sticky_nav_container.css({ 'height':sticky_nav.outerHeight() });
        sticky_nav.addClass("sticky")
          .stop()
          .css("top",-sticky_nav.outerHeight())
          .animate({"top":sticky_top_spacing});
      } else {
        sticky_nav_container.css({ 'height':'auto' });
        sticky_nav.removeClass('sticky')
          .stop()
          .css('top',sticky_nav.outerHeight()+waypoint_offset)
          .animate({"top":""});
      }
    },
    offset: function () {
      return -sticky_nav.outerHeight()-waypoint_offset;
    }
  });

  var sticky_sections = $('section');
  var sticky_navigation_links = $('nav a');

  sticky_sections.waypoint({
    handler: function( event, direction ) {
      var active_section;
      active_section = $(this);
      if(direction === "up") active_section = active_section.prev();

      var active_link = $('nav a[href="#' + active_section.attr("id") + '"]');
      sticky_navigation_links.removeClass("active");
      active_link.addClass("active");
    },
    offset: '25%'
  });

  sticky_navigation_links.click( function(event) {
    $.scrollTo(
      $(this).attr("href"),
      {
        duration: 200,
        offset: { 'left':0, 'top':-0.15*$(window).height() }
      }
    );
  });

  var waypoint_nav = $('a.waypoint');
  waypoint_nav.click( function(event) {
    $.scrollTo(
      $(this).attr("href"),
      {
        duration: 200,
        offset: { 'left':0, 'top':-0.15*$(window).height() }
      }
    );
  });

  function waypoint_scroll(event) {
    $.scrollTo(
      $(this).attr("href"),
      {
        duration: 200,
        offset: { 'left':0, 'top':-0.15*$(window).height() }
      }
    );
  }

  // --------------------------------------------------
  // HOVERINTENT on nav#main
  // --------------------------------------------------
  mainnav = $('nav#main li.has-flyout');
  hoverintentconfig = {
    over: openNav,
    timeout: 500,
    out: closeNav
  }
  mainnav.hoverIntent( hoverintentconfig );
  function openNav(){  $(this).find("> ul.flyout").slideDown( 375, 'swing' ); };
  function closeNav(){ $(this).find("> ul.flyout").slideUp( 375, 'swing' ); };

  // --------------------------------------------------
  // Toolbox script
  // --------------------------------------------------
  var mytoolbox = document.getElementById('toolbox');
  var mytoolboxList = document.getElementById('toolboxList');
  var mytoolboxLinks = mytoolboxList.getElementsByTagName('a');
  for(var i = 0, len = mytoolboxLinks.length; i < len; ++i) {
    mytoolboxLinks[i].onclick = function() {
      $(this).toggleClass('open');
      var boxID = this.getAttribute('data-toolbox-id'); // this is an IE safe way to grab the dataset attribute
      var box = $('#'+boxID);
      box.slideToggle('fast','swing');
    };
  }

});
